import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    redirect:{name:'regist'},
    component: Home
  },
  {
    path: '/login',
    name: 'login',
    component: () => import( '../views/Administration/login.vue'),
  },
  {
    path: '/admin',
    name: 'admin',
    component: () => import( '../views/Administration/admin.vue'),
    // beforeEnter:(to, from, next)=>{//路由独享守卫 三个参数固定的
    //   //从local里面读取有没有user字段 如果有 就正常跳转， 没有就拦截
    //   if(!localStorage.getItem('user')){
    //     next('/login')
    //   }
    //   console.log('路由独享守卫to', to)
    //   console.log('路由独享守卫from', from)
    //   next()
    // }
  },
  {
    path: '/regist',
    name: 'regist',
    component: () => import( '../views/Administration/regist.vue')
  },
  {
    path: '/about',
    name: 'About',
    component: () => import( '../views/About.vue')
  }
]
const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  if (!localStorage.getItem('user')) {
      if (to.path !== '/login') {
          return  next('/login')
      }
  }
  next()
})

export default router
